package org.geektimes.projects.user.management.mbean;

import com.sun.jmx.mbeanserver.Introspector;
import org.geektimes.projects.user.domain.User;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;

/**
 * @Author: jipeng
 * @Description:
 * @Date: Created in 2021/3/15 22:41
 */
public class UserMBeanDemo {

    public static void main(String[] args) throws Exception {
        Introspector.testCompliance(UserManager.class);

        //获取平台 mbean Server
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        //为 UserMBean 定义 ObjectName
        ObjectName objectName = new ObjectName("org.geektimes.projects.user.management:type=UserService");
        //创建 UserMBean 实例
        User user = new User();
        //注册 UserMBean
        mBeanServer.registerMBean(createUserMXBean(user), objectName);

        while(true){
            Thread.sleep(2000);
            System.out.println(user);
        }

    }

    private static Object createUserMXBean(User user) {
        return new UserManager(user);
    }


}
